package com.crealth.manager;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.crealth.Config;
import com.crealth.db.UserDb;
import com.crealth.mapper.BankChannelMapper;
import com.crealth.mapper.BankSpecialLimitMapper;
import com.crealth.mapper.CdrMapper;
import com.crealth.mapper.CityDistrictMapper;
import com.crealth.mapper.ClienteleInfoMapper;
import com.crealth.mapper.CompanyMapper;
import com.crealth.mapper.ConsumerInfoMapper;
import com.crealth.mapper.ConsumerProductMapper;
import com.crealth.mapper.CreditReportMapper;
import com.crealth.mapper.EventRemindMapper;
import com.crealth.mapper.KnowledgeCreditLoanMapper;
import com.crealth.mapper.KnowledgePledgetLoanMapper;
import com.crealth.mapper.LendingAgencyMapper;
import com.crealth.mapper.LoanAmountCalculatedMapper;
import com.crealth.mapper.LoanContractMapper;
import com.crealth.mapper.MobileAttributionMapper;
import com.crealth.mapper.MonthPlanMapper;
import com.crealth.mapper.MyselfClienteleInfoMapper;
import com.crealth.mapper.PledgeDiYaMapper;
import com.crealth.mapper.SendBackEarnestMapper;
import com.crealth.mapper.SipConfigMapper;
import com.crealth.mapper.StaffDayKPIMapper;
import com.crealth.mapper.SupportedVoiceCodecsMapper;
import com.crealth.mapper.TeamMapper;
import com.crealth.mapper.TeamWorkDiaryMapper;
import com.crealth.mapper.UserMapper;
import com.crealth.mapper.WeekPlanMapper;
import com.crealth.mapper.WhiteListMapper;


public class MybatisManager {
	private static ApplicationContext ctx; 
    private static SqlSessionFactory sqlSessionFactory;
    public static SqlSession sqlSession;
    
    public static UserMapper userMapper;
	public static ClienteleInfoMapper clienteleMapper;
	public static CdrMapper cdrMapper;
	public static CompanyMapper companyMapper;
	public static WhiteListMapper whitelistMapper;
	public static ConsumerInfoMapper consumerMapper;
	public static MobileAttributionMapper mobileMapper;
	public static StaffDayKPIMapper dayKPIMapper;
	public static TeamMapper teamMapper;
	public static LoanContractMapper contractMapper;
	public static ConsumerProductMapper productMapper;
	public static SupportedVoiceCodecsMapper voiceCodecsMapper;
	public static PledgeDiYaMapper diyaMapper;
	public static WeekPlanMapper weekPlanMapper;
	public static MonthPlanMapper monthPlanMapper;
	public static SendBackEarnestMapper earnestMapper;
	public static BankChannelMapper bankChannelMapper;
	public static LoanAmountCalculatedMapper amountCalculatedMapper;
	public static BankSpecialLimitMapper specialLimitMapper;
	public static KnowledgeCreditLoanMapper creditKnowledgeMapper;
	public static KnowledgePledgetLoanMapper pledgetKnowledgeMapper;
	public static TeamWorkDiaryMapper workDiaryMapper;
	public static CityDistrictMapper ciryDistrictMapper;
	public static LendingAgencyMapper lendingAgencyMapper;
	public static MyselfClienteleInfoMapper myselfClienteleInfoMapper;
	public static CreditReportMapper creditReportMapper;
	public static SipConfigMapper sipConfigMapper;
	public static EventRemindMapper eventRemindMapper;

    public static void initSessionFactory() throws IOException {
    	org.apache.ibatis.logging.LogFactory.useLog4JLogging();
    	
      	ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
      	sqlSessionFactory = (SqlSessionFactory)ctx.getBean("sqlSessionFactory");
    	if (sqlSessionFactory != null) {
    		sqlSession = sqlSessionFactory.openSession();
    	}
      	
      	userMapper = ctx.getBean(UserMapper.class);
        clienteleMapper = ctx.getBean(ClienteleInfoMapper.class);
        cdrMapper = ctx.getBean(CdrMapper.class);            
        companyMapper = ctx.getBean(CompanyMapper.class);
        whitelistMapper = ctx.getBean(WhiteListMapper.class);
        consumerMapper = ctx.getBean(ConsumerInfoMapper.class);
        mobileMapper = ctx.getBean(MobileAttributionMapper.class);
        dayKPIMapper = ctx.getBean(StaffDayKPIMapper.class);
        teamMapper = ctx.getBean(TeamMapper.class);
        contractMapper = ctx.getBean(LoanContractMapper.class);
        productMapper = ctx.getBean(ConsumerProductMapper.class);
        voiceCodecsMapper = ctx.getBean(SupportedVoiceCodecsMapper.class);
        diyaMapper = ctx.getBean(PledgeDiYaMapper.class);
        weekPlanMapper = ctx.getBean(WeekPlanMapper.class);
        earnestMapper = ctx.getBean(SendBackEarnestMapper.class);
        monthPlanMapper = ctx.getBean(MonthPlanMapper.class);
        bankChannelMapper = ctx.getBean(BankChannelMapper.class);
        amountCalculatedMapper = ctx.getBean(LoanAmountCalculatedMapper.class);
        specialLimitMapper = ctx.getBean(BankSpecialLimitMapper.class);
        creditKnowledgeMapper = ctx.getBean(KnowledgeCreditLoanMapper.class);
        pledgetKnowledgeMapper = ctx.getBean(KnowledgePledgetLoanMapper.class);
        workDiaryMapper = ctx.getBean(TeamWorkDiaryMapper.class);
        ciryDistrictMapper = ctx.getBean(CityDistrictMapper.class);
        lendingAgencyMapper = ctx.getBean(LendingAgencyMapper.class);
        myselfClienteleInfoMapper = ctx.getBean(MyselfClienteleInfoMapper.class);
        creditReportMapper = ctx.getBean(CreditReportMapper.class);
        sipConfigMapper = ctx.getBean(SipConfigMapper.class);
        eventRemindMapper = ctx.getBean(EventRemindMapper.class);
        
        ClienteleInfoManager.initMaxID();
        
        Integer isShow = UserDb.selectIsShowConsumerInfo();
        if (isShow != null) {
        	Config.showSearchConsumerInfo = isShow;
        } else {
        	Config.showSearchConsumerInfo = 1;
        }
        Integer sipType = UserDb.selectSipDispatchType();
        if (sipType != null) {
        	Config.sipDispatchType = sipType;
        } else {
        	Config.sipDispatchType = 0;
        }
        SipAccountManager.loadSipConfig();
    }
    
    public static void commit() {
    	sqlSessionFactory = (SqlSessionFactory)ctx.getBean("sqlSessionFactory");
    	if (sqlSessionFactory != null) {
    		sqlSession = sqlSessionFactory.openSession();
    		sqlSession.commit();
    	}
    }
    
    
    
}

